package com.tiza.xiaoyoubao.ui.fragment;

import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.BankNameInfo;
import com.tiza.xiaoyoubao.entity.EditInfo;

import org.simple.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 * Created by mapple.
 */
public class BankNameFragment extends BaseFragment {

    private static List<BankNameInfo> mCityInfos;
    @BindView(R.id.lv_listView)
    ListView mListView;
    private CityAdapter mCityAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_get_bank_name;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        initCity();
        mCityAdapter = new CityAdapter();
        mListView.setAdapter(mCityAdapter);
        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String cityName = mCityInfos.get(position).getBankName();
                EventBus.getDefault().post(new EditInfo(EditInfo.TYPE_GET_BANK_NAME, cityName),
                                           Constants.TAG_GETCITY_SUCCESS
                );

                finish();
            }
        });
    }

    class CityAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mCityInfos.size();
        }

        @Override
        public Object getItem(int position) {
            return mCityInfos.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                convertView = View.inflate(getContext(), R.layout.item_city_info, null);
                holder = new ViewHolder();
                holder.tvBankName = (TextView) convertView.findViewById(R.id.tv_City);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.tvBankName.setText(mCityInfos.get(position).getBankName());
            return convertView;
        }
    }

    static class ViewHolder {

        public TextView tvBankName;
    }

    private void initCity() {
        mCityInfos = new ArrayList<>();
        mCityInfos.add(new BankNameInfo("中国工商银行"));
        mCityInfos.add(new BankNameInfo("中国农业银行"));
        mCityInfos.add(new BankNameInfo("中国建设银行"));
        mCityInfos.add(new BankNameInfo("中国银行"));
        mCityInfos.add(new BankNameInfo("交通银行"));
        mCityInfos.add(new BankNameInfo("招商银行"));

    }
}
